package cn.dqy.gotanserver.code;

import cn.dqy.gotanserver.dto.GoTanObject;
import com.google.gson.Gson;
import jdk.nashorn.internal.objects.Global;

import java.nio.charset.Charset;

public class CodeUtils {
    public static byte[] CUT_COMMAND=new byte[]{0,0,0,1};
    public static byte[] ZERO_PIC=new byte[]{0};

    public static Charset  globalCharSet;
    static {
        globalCharSet = Charset.forName("gbk");
    }

    /*小端，低字节在后*/
    public static int bytesToIntLittleEndian(byte[] bytes) {
        // byte数组中序号小的在右边
        return bytes[0] & 0xFF | //
                (bytes[1] & 0xFF) << 8 | //
                (bytes[2] & 0xFF) << 16 | //
                (bytes[3] & 0xFF) << 24; //
    }
    /*大端，高字节在后*/
    public static int bytesToIntBigEndian(byte[] bytes) {
        // byte数组中序号大的在右边
        return bytes[3] & 0xFF | //
                (bytes[2] & 0xFF) << 8 | //
                (bytes[1] & 0xFF) << 16 | //
                (bytes[0] & 0xFF) << 24; //
    }

    public static byte[] buildGoTanObjectToJsonBytes(int command, String remark,
                                                     boolean hasFile, boolean nameIsTrue,
                                                     String name, String fixName){
        GoTanObject object=new GoTanObject();
        object.setCommand(command);
        object.setRemark(remark);
        object.setHasFile(hasFile);
        object.setNameIsTrue(nameIsTrue);
        object.setFileName(name);
        object.setFileFixName(fixName);
        String json = new Gson().toJson(object);
        byte[] bytes = json.getBytes(CodeUtils.globalCharSet); //由于客户端为windows系统，发送中文使用gbk
        return bytes;
    }

    public static byte[] buildGoTanObjectToJsonBytes(int command, String remark){
        GoTanObject object=new GoTanObject();
        object.setCommand(command);
        object.setRemark(remark);
        String json = new Gson().toJson(object);
        byte[] bytes = json.getBytes(CodeUtils.globalCharSet); //由于客户端为windows系统，发送中文使用gbk
        return bytes;
    }


}
